var http = require("http");
var items = [];
var server = http.createServer(function(req,res){
    res.setHeader("Content-Type","text/html");
    switch(req.method){
        case "GET":
            res.write("<html><head></head><body>");
            items.forEach(function(item,i){
                res.write(i+") " + item + "<br/>");
            });
            res.write('<form action="http://localhost:8899/" method="post"><input name="item"><button type="submit">submit</button></form>');
            res.write("</body></html>");
            res.end();
            break;
        case "POST":
            console.log("post");
            var item = "";
            req.setEncoding("utf-8");
            req.on("data",function(data){
                console.log(data);
                item+=data.replace("item=","");
            });
            req.on("end",function(){
               items.push(item);
            });
            var url = "http://localhost:8899/";
            res.setHeader("Location",url);
            res.setHeader("Content-Type","text/html");
            res.statusCode = 302;
            res.end();
            break;
    }
});
server.listen(8899);